package com.mmall.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.ExceptionError;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysAclMapper;
import com.mmall.model.SysAcl;
import com.mmall.param.AclParam;
import com.mmall.util.BeanVaildator;
import com.mmall.util.IpUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysAclService {

    @Resource
    private SysAclMapper mapper;
    @Resource
    private SysLogService sysLogService;

    public void save(AclParam param){
        BeanVaildator.check(param);
        if (checkExist(param.getAclModuleId(),param.getName(),param.getId(),param.getUrl())){
            ExceptionError.throwError("当前权限模块下面存在相同名称的权限点");
        }
        SysAcl acl = SysAcl.builder().name(param.getName())
                .operator(RequestHolder.getCurrentUser().getUsername())
                .aclModuleId(param.getAclModuleId())
                .operateTime(new Date())
                .seq(param.getSeq())
                .remark(param.getRemark())
                .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()))
                .status(param.getStatus())
                .code(generateCode())
                .type(param.getType())
                .url(param.getUrl()).build();

        mapper.insertSelective(acl);
        sysLogService.saveAclLog(null,acl);

    }

    public void update(AclParam param){
        BeanVaildator.check(param);
        if (checkExist(param.getAclModuleId(),param.getName(),param.getId(),param.getUrl())){
            ExceptionError.throwError("当前权限模块下面存在相同名称的权限点");
        }
        SysAcl before = mapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待更新的权限点不存在");

        SysAcl acl = SysAcl.builder().id(param.getId()).name(param.getName())
                .operator(RequestHolder.getCurrentUser().getUsername())
                .aclModuleId(param.getAclModuleId())
                .operateTime(new Date())
                .seq(param.getSeq())
                .remark(param.getRemark())
                .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()))
                .status(param.getStatus())
                .type(param.getType())
                .url(param.getUrl()).build();

        mapper.updateByPrimaryKeySelective(acl);
        sysLogService.saveAclLog(before,acl);
    }

    public boolean checkExist(Integer aclModuleId, String name, Integer id,String url) {
        return mapper.countByNameAndAclModuleId(aclModuleId, name, id,url) > 0;
    }

    /**
     * 生成权限编码
     * @return
     */
    public String generateCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int)(Math.random() * 100);
    }

    public PageResult<SysAcl> getPageByAclModuleId(Integer aclModuleId, PageQuery pageQuery) {
        BeanVaildator.check(pageQuery);
        int count = mapper.countByAclModuleId(aclModuleId);
        if (count>0){
            List<SysAcl> list = mapper.getPageByAclModuleId(aclModuleId,pageQuery);
            return PageResult.<SysAcl>builder().total(count).data(list).build();
        }
        return PageResult.<SysAcl>builder().build();
    }

    public void batchSave(Set<String> aclSets) {
        List<SysAcl> aclList = Lists.newLinkedList();
        for(String acl : aclSets){
            SysAcl sysAcl = SysAcl.builder().name(acl.substring(0,acl.indexOf(","))).url(acl.substring(acl.indexOf(","),acl.length()))
                    .aclModuleId(0).build();
        }
    }
}
